package com.example.redisdemo.listener;


import com.example.redisdemo.configuration.RabbitConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * @author hexinglin
 * @created 2021-08-11 14:41
 **/
@Component
@Slf4j
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = "status", havingValue = "true")
public class RabbitListeners {

    @RabbitListener(queues ="direct" )
    public void receiverdirect( Message message, Channel channel) throws InterruptedException {
      //  Thread.sleep(500L); // 模拟处理需要2秒
        System.out.println("消费者消费。。。。。。{}"+ message.getMessageProperties());
        // 消息删除
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            System.out.println("消息删除失败");
        }
    }
    @RabbitListener(queues= RabbitConfig.QUEUE_TEST1)
    public void getDirectMessage(Channel channel, Message message) throws IOException {
        log.info("【mq接收到的消息为：】,{}",new String(message.getBody()));
        try {
            // 模拟执行任务
            Thread.sleep(1000);
            // 模拟异常
            String is = null;
            //is.toString();
            // 确认收到消息，false只确认当前consumer一个消息收到，true确认所有consumer获得的消息
          //  channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                log.info("消息已重复处理失败,拒绝再次接收");
                // 拒绝消息，requeue=false 表示不再重新入队，如果配置了死信队列则进入死信队列
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                log.info("消息即将再次返回队列处理");
                // requeue为是否重新回到队列，true重新入队
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
            //e.printStackTrace();
        }
    }
    @RabbitListener(queues= RabbitConfig.RECEIVE_QUEUE)
    public void getReceiveQueue(Channel channel, Message message) throws IOException {
        log.info("【mq接收到的消息为：】,{}",new String(message.getBody()));
        log.info("【mq接收到的时间为：】,{}", LocalDateTime.now());
        try {
            // 模拟执行任务
            Thread.sleep(1000);
            // 模拟异常
            String is = null;
            //is.toString();
            // 确认收到消息，false只确认当前consumer一个消息收到，true确认所有consumer获得的消息
            //  channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            if (message.getMessageProperties().getRedelivered()) {
                log.info("消息已重复处理失败,拒绝再次接收");
                // 拒绝消息，requeue=false 表示不再重新入队，如果配置了死信队列则进入死信队列
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                log.info("消息即将再次返回队列处理");
                // requeue为是否重新回到队列，true重新入队
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
            //e.printStackTrace();
        }
    }

}
